﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SharedLibraries;
//using System.Web.Routing;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace CSharp
{
    public partial class Default : System.Web.UI.Page
    {

        public List<URLRewriterEO> lstMapUrlTemp
        {
            get { return (List<URLRewriterEO>)ViewState["lstMapUrlTemp"]; }
            set { ViewState["lstMapUrlTemp"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            txtHtml5.Text = DateTime.Now.ToString();
            if (!IsPostBack)
            {
                List<URLRewriterEO> lstTemp = new List<URLRewriterEO>();
                lstTemp = (List<URLRewriterEO>)JsonEx.Json2ListObject(Folder_File.ReadFile("~/MapUrl.json", CSharp.Properties.Settings.Default.PathRoot), lstTemp.GetType());
                lstMapUrlTemp = lstTemp;
                BindData();
                txtHtml5.Attributes.Add("type", "number");
            }
        }

        protected void BindData()
        {
            lblMsg.Text = "";
            if (lstMapUrlTemp == null || lstMapUrlTemp.Count < 1)
            {
                lstMapUrlTemp = new List<URLRewriterEO>();
                lstMapUrlTemp.Insert(0, new URLRewriterEO { routeName = "", routeUrl = "", physicalFile = "" });
            }
            if (lstMapUrlTemp.Count > 1)
            {
                var result = lstMapUrlTemp.FindAll(x => !String.IsNullOrEmpty(x.routeName));
                lstMapUrlTemp = result;
            }
            grvList.DataSource = lstMapUrlTemp.ToList();
            grvList.DataBind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                TextBox txtrouteName_Temp = (TextBox)grvList.FooterRow.FindControl("txtrouteName_Temp");
                TextBox txtrouteUrl_Temp = (TextBox)grvList.FooterRow.FindControl("txtrouteUrl_Temp");
                TextBox txtphysicalFile_Temp = (TextBox)grvList.FooterRow.FindControl("txtphysicalFile_Temp");
                if (String.IsNullOrEmpty(StringEx.Trim(txtrouteName_Temp.Text)))
                {
                    BindData();
                    lblMsg.Text = "routeName bắt buộc phải nhập.";
                    txtrouteName_Temp.Focus();
                    return;
                }
                else if ((String.IsNullOrEmpty(StringEx.Trim(txtphysicalFile_Temp.Text))) || (StringEx.Trim(txtphysicalFile_Temp.Text).StartsWith("~/") == false))
                {
                    BindData();
                    lblMsg.Text = "physicalFile bắt buộc phải nhập va bat dau bang '~/'.";
                    txtphysicalFile_Temp.Focus();
                    return;
                }
                else
                {
                    var duplicate = lstMapUrlTemp.Find(n => StringEx.Equals(n.routeName, StringEx.Trim(txtrouteName_Temp.Text)));
                    if (duplicate != null)
                    {
                        BindData();
                        lblMsg.Text = "routeName không được trùng.";
                        txtrouteName_Temp.Focus();
                        return;
                    }
                }

                URLRewriterEO objLink = new URLRewriterEO();
                objLink.routeName = StringEx.Trim(txtrouteName_Temp.Text);
                objLink.routeUrl = StringEx.Trim(txtrouteUrl_Temp.Text);
                objLink.physicalFile = StringEx.Trim(txtphysicalFile_Temp.Text);

                lstMapUrlTemp.Insert(0, objLink);
                txtrouteName_Temp.Text = "";
                txtrouteUrl_Temp.Text = "";
                txtphysicalFile_Temp.Text = "";

                BindData();
                lblMsg.Text = "Thêm thành công.";
                txtrouteName_Temp.Focus();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

        protected void grvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDelete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                lstMapUrlTemp.RemoveAt(id);
                BindData();
                lblMsg.Text = "Xóa thành công.";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstMapUrlTemp != null && lstMapUrlTemp.Count > 0)
                {
                    Folder_File.WriteFile("~/MapUrl.json", JsonEx.ListObject2Json(lstMapUrlTemp));
                    //URLRewriter.bSyncMapUrl = true;
                    lblMsg.Text = "cap nhat thanh cong.";
                }
                else
                {
                    lblMsg.Text = "chưa nhập ds map.";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

        protected void btnCheckRouteTable_Click(object sender, EventArgs e)
        {
            try
            {
                //lblMsg.Text = RouteTable.Routes.Count.ToString();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            txtHtml5.Text = Convert.ToDateTime("28/08/1992").ToString("yyyy-MM-dd");
        }
    }
}